package chabernac.android.wordbattle.paint.bogglemerge;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.View;
import chabernac.android.wordbattle.R;
import chabernac.android.wordbattle.WordBattleView;
import chabernac.android.wordbattle.WordRasterContext;
import chabernac.android.wordbattle.model.Letter;
import chabernac.android.wordbattle.paint.iDrawable;

import com.larvalabs.svgandroid.SVG;
import com.larvalabs.svgandroid.SVGParser;

public class BoggleMergeLetterDrawable extends iDrawable {
  private Picture myBox;
  private final Letter myLetter;
  private WordBattleView myWordBattleView;
  private final Paint myPaint = new Paint();

  public BoggleMergeLetterDrawable(Letter aLetter){
    myLetter = aLetter;
    init();
  }

  private void init(){
    SVG svg = SVGParser.getSVGFromResource(WordRasterContext.getInstance().getContext().getResources(), R.raw.letterbox);
    myBox = svg.getPicture();

    myPaint.setAntiAlias( true );
  }

  @Override
  public void paint(Canvas aCanvas, View aView) {
    myWordBattleView = (WordBattleView)aView;
    int theX = WordBattleView.BORDER_SIZE + (int)(myLetter.getColumn() * myWordBattleView.getWidthPerColumn());
    int theY = WordBattleView.BORDER_SIZE + (int)(myLetter.getRow() * myWordBattleView.getHeightPerRow());


    //draw the background
    RectF theRect = new RectF(theX + WordBattleView.BORDER_SIZE, theY + WordBattleView.BORDER_SIZE, theX + myWordBattleView.getWidthPerColumn() - 2 * WordBattleView.BORDER_SIZE, theY + myWordBattleView.getHeightPerRow() - 2 * WordBattleView.BORDER_SIZE);
//    long t1 = System.nanoTime();
    aCanvas.drawPicture( myBox, theRect);
//    long t2 = System.nanoTime();
//    Log.d(BoggleMergeLetterDrawable.class.getName(), "Drawing letter background took '" + (t2-t1) + "' ns");


    //draw the letter
    //set text size
    float theMinSize = (float)(Math.min(myWordBattleView.getHeightPerRow(), myWordBattleView.getWidthPerColumn()) * 0.6 );
    myPaint.setTextSize(theMinSize);

    //calculate where to position the letter
    Rect theFontBounds = new Rect();
    myPaint.getTextBounds(myLetter.getLetter(), 0 , 1 , theFontBounds);

    float theFontWidth = theFontBounds.width();
    float theFontHeight = theFontBounds.height();

    float theXOffset = (myWordBattleView.getWidthPerColumn() - theFontWidth) / 2F;
    float theYOffset = (myWordBattleView.getWidthPerColumn() - theFontHeight) / 2F;

    float theXL = theX + theXOffset;
    float theYL = theY +  theYOffset + theFontHeight; 

    myPaint.setShader(null);
    myPaint.setStyle( Paint.Style.FILL );
    myPaint.setColor(Color.BLACK);
    myPaint.setAlpha( 200 );
    myPaint.setFakeBoldText( true );

    //draw the letter
    aCanvas.drawText(myLetter.getLetter(), theXL - theFontBounds.left, theYL - theFontBounds.bottom , myPaint);

  }

}
